#include "MainWindow.h"
#include "EditorObjectWidget.h"

EditorObjectWidget::EditorObjectWidget(MainWindow *parent, Qt::WindowFlags flags)
    : QDockWidget(tr("Object Viewer"), parent, flags),
      mpMainWindow(parent),
      mpContents(0),
      mpLayout(0),
      mpTreeWidget(0)
{
    setMinimumSize(QSize(220, 220));

    mpContents = new QWidget(this);
    setWidget(mpContents);
    mpLayout = new QVBoxLayout(mpContents);
    mpContents->setLayout(mpLayout);
    mpTreeWidget = new QTreeWidget(mpContents);
    mpLayout->addWidget(mpTreeWidget);

    QTreeWidgetItem *mpheaderItem = mpTreeWidget->headerItem();
    mpheaderItem->setText(0, tr("Object"));
    mpheaderItem->setText(1, tr("Name"));
}

EditorObjectWidget::~EditorObjectWidget()
{
}

void EditorObjectWidget::setModel(const ne::ModelPtr &pModel)
{
    mpModel = pModel;
    updateTreeWidget();
}

ne::ModelPtr EditorObjectWidget::getModel() const
{
    return mpModel;
}

void EditorObjectWidget::updateTreeWidget()
{
    clearTreeWidget();

    if (mpModel.notNull())
    {
        QTreeWidgetItem *pModelItem = new QTreeWidgetItem(mpTreeWidget);
        pModelItem->setText(0, tr("Model"));
        pModelItem->setText(1, QString(mpModel->getName().c_str()));
        pModelItem->setExpanded(true);

        ne::SkeletonPtr pSkeleton = mpModel->getSkeleton();
        if (pSkeleton.notNull() && pSkeleton->getNumJoints())
        {
            QTreeWidgetItem *pSkeletonItem = new QTreeWidgetItem(pModelItem);
            pSkeletonItem->setText(0, tr("Skeleton"));
            pSkeletonItem->setText(1, QString(pSkeleton->getName().c_str()));
        }

        ne::AnimationGroupPtr pAnimationGroup = mpModel->getAnimationGroup();
        if (pAnimationGroup.notNull())
        {
            QTreeWidgetItem *pAnimationGroupItem = new QTreeWidgetItem(pModelItem);
            pAnimationGroupItem->setText(0, tr("AnimationGroup"));
            pAnimationGroupItem->setText(1, QString(pAnimationGroup->getName().c_str()));
        }

        const ne::SkinMap &skinMap = mpModel->getSkinMap();
        ne::SkinMapConstIterator it, iend;
        iend = skinMap.end();
        for (it=skinMap.begin(); it!=iend; ++it)
        {
            ne::Skin *pSkin = it->second;
            if (pSkin)
            {
                QTreeWidgetItem *pSkinItem = new QTreeWidgetItem(pModelItem);
                pSkinItem->setText(0, tr("Skin"));
                pSkinItem->setText(1, QString(pSkin->getObjectName().c_str()));
                pSkinItem->setExpanded(true);

                ne::MeshPtr pMesh = pSkin->getMesh();
                if (pMesh.notNull())
                {
                    QTreeWidgetItem *pMeshItem = new QTreeWidgetItem(pSkinItem);
                    pMeshItem->setText(0, tr("Mesh"));
                    pMeshItem->setText(1, QString(pMesh->getName().c_str()));
                    pMeshItem->setExpanded(true);

                    ne::TexturePtr pTexture = pMesh->getTexture();
                    if (pTexture.notNull())
                    {
                        QTreeWidgetItem *pTextureItem = new QTreeWidgetItem(pMeshItem);
                        pTextureItem->setText(0, tr("Texture"));
                        pTextureItem->setText(1, QString(pTexture->getName().c_str()));
                    }

                    ne::TexturePtr pBumpMap = pMesh->getBumpMap();
                    if (pBumpMap.notNull())
                    {
                        QTreeWidgetItem *pBumpMapItem = new QTreeWidgetItem(pMeshItem);
                        pBumpMapItem->setText(0, tr("BumpMap"));
                        pBumpMapItem->setText(1, QString(pBumpMap->getName().c_str()));
                    }

                    const ne::SubMeshMap &subMeshMap = pMesh->getChildrenMap();
                    ne::SubMeshMapConstIterator iMesh, iMeshEnd;
                    iMeshEnd = subMeshMap.end();
                    for (iMesh=subMeshMap.begin(); iMesh!=iMeshEnd; ++iMesh)
                    {
                        ne::SubMeshPtr pSubMesh = iMesh->second;
                        if (pSubMesh.notNull())
                        {
                            QTreeWidgetItem *pSubMeshItem = new QTreeWidgetItem(pMeshItem);
                            pSubMeshItem->setText(0, tr("SubMesh"));
                            pSubMeshItem->setText(1, QString(pSubMesh->getObjectName().c_str()));
                            pSubMeshItem->setExpanded(true);

                            ne::MaterialPtr pMaterial = pSubMesh->getMaterial();
                            if (pMaterial.notNull())
                            {
                                QTreeWidgetItem *pMaterialItem = new QTreeWidgetItem(pSubMeshItem);
                                pMaterialItem->setText(0, tr("Material"));
                                pMaterialItem->setText(1, QString(pMaterial->getName().c_str()));
                            }
                        }
                    }
                }
            }
        }
    }
}

void EditorObjectWidget::clearTreeWidget()
{
    mpTreeWidget->clear();
}
